//
//  ShoppingCartCell.m
//  wtshoppingcite
//
//  Created by TW on 14-11-12.
//  Copyright (c) 2014年 TW. All rights reserved.
//

#import "ShoppingCartCell.h"
#import "RegisterHelper.h"
#import "ShoppingCartViewController.h"
@implementation ShoppingCartCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
    }
    return self;
}

-(void)createSubViews{
    
    
    
    _iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(30, 5, 100, 90)];
    _iconImageView.image = [UIImage imageNamed:@""];
    [self.contentView addSubview:_iconImageView];
    
    _titleLable = [self createLabelWithFrame:CGRectMake(_iconImageView.right+10, 0, 120, 40) text:@"" font:17 textAli:NSTextAlignmentLeft textColor:UIColorFromRGB(0x9fa0a0)];
    _titleLable.numberOfLines = 0;
    [self.contentView addSubview:_titleLable];
    
    _iconImage = [[UIImageView alloc]initWithFrame:CGRectMake(_titleLable.right, 20, 10, 10)];
    _iconImage.image = [UIImage imageNamed:@""];
    [self.contentView addSubview:_iconImage];
    
    _sPriceLable = [self createLabelWithFrame:CGRectMake(_iconImage.right, _iconImage.top-20, 45, 45) text:@"" font:16 textAli:NSTextAlignmentLeft textColor:MAIN_BLUE_COLOR];
    [self.contentView addSubview:_sPriceLable];
    
    _seprateLable = [[AttrPriceLabel alloc] initWithFrame:CGRectMake(_sPriceLable.left - 10, _sPriceLable.bottom -10, 60, 20)];
    _seprateLable.font = [UIFont systemFontOfSize:12.0f];
    [self.contentView addSubview:_seprateLable];
    
    _sNum = [self createLabelWithFrame:CGRectMake(_iconImageView.right + 10, _titleLable.bottom, 100, 30) text:@"" font:16 textAli:NSTextAlignmentLeft textColor:UIColorFromRGB(0x9fa0a0)];
    [self.contentView addSubview:_sNum];
    
    _subtractButton = [[UIButton alloc]initWithFrame:CGRectMake(_sNum.left+20, _sNum.bottom +5, 35, 35)];
    [_subtractButton setImage:[UIImage imageNamed:@"near_subtractButton"] forState:UIControlStateNormal];
    _subtractButton.tag = 100;
    [_subtractButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_subtractButton];
    
    _textField = [[UITextField alloc]initWithFrame:CGRectMake(_subtractButton.right, _subtractButton.top, 60, 35)];
    _textField.textAlignment = NSTextAlignmentCenter;
    _textField.borderStyle = UITextBorderStyleRoundedRect;
    [self.contentView addSubview:_textField];
    
    _addButton = [[UIButton alloc]initWithFrame:CGRectMake(_textField.right,_textField.top, 35, 35)];
    [_addButton setImage:[UIImage imageNamed:@"addbutton"] forState:UIControlStateNormal];
    
    _addButton.tag = 101;
    [_addButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_addButton];
    
}

-(void)buttonAction:(UIButton *)sender{
    
    if (sender.tag == 100 && self.i-1 != 0) {
        
        [_subtractButton setImage:[UIImage imageNamed:@"subtractButton_select1"] forState:UIControlStateNormal];
        [_addButton setImage:[UIImage imageNamed:@"addbutton"] forState:UIControlStateNormal];
        self.i = self.i - 1;
        int num = [RegisterHelper shareHelper].budgeNum.integerValue;
        num--;
        [[RegisterHelper shareHelper] setBudgeNum:[NSString stringWithFormat:@"%d",num]];
        
    }else if (sender.tag == 101){
        
        [_addButton setImage:[UIImage imageNamed:@"addButton_select"] forState:UIControlStateNormal];
        [_subtractButton setImage:[UIImage imageNamed:@"near_subtractButton"] forState:UIControlStateNormal];
        self.i = self.i + 1;
        int num = [RegisterHelper shareHelper].budgeNum.integerValue;
        num++;
        [[RegisterHelper shareHelper] setBudgeNum:[NSString stringWithFormat:@"%d",num]];
    }
    
    NSMutableDictionary* shopCarNumbers = [[RegisterHelper shareHelper] shopCarNumbers];
    NSString* key = _shoppingModel.sid;
    NSString* value = [NSString stringWithFormat:@"%d",self.i] ;
    [shopCarNumbers setObject:value forKey:key];
    [[RegisterHelper shareHelper] setShopCarNumbers:shopCarNumbers];
    
    _textField.text = [NSString stringWithFormat:@"%d",self.i];
  
     _sNum.text =[NSString stringWithFormat:@"数量:%d",self.i];
}


-(void)setShoppingModel:(ShoppingCartModel *)shoppingModel{
    
    if (_shoppingModel != shoppingModel) {
        _shoppingModel = shoppingModel;
        
        [_iconImageView setImageWithURL:[NSURL URLWithString:self.shoppingModel.sPic] placeholderImage:[UIImage imageNamed:@"all_defalutImage"]];
        _iconImage.image = [UIImage imageNamed:@"money_code"];
        _titleLable.text = self.shoppingModel.title;
        _sPriceLable.text = self.shoppingModel.oPrice ;
        
        NSMutableDictionary* shopCarNumbers = [[RegisterHelper shareHelper] shopCarNumbers];
        NSString* num = [shopCarNumbers objectForKey:self.shoppingModel.sid];
        self.i = num.integerValue;
        
        _sNum.text =[NSString stringWithFormat:@"数量:%d", self.i];
        _textField.text = [NSString stringWithFormat:@"%d",self.i];
        [_seprateLable setPriceType:2 text:shoppingModel.nPrice];
    }
    
    if(_isEidt == YES){
        _addButton.hidden = NO;
        _subtractButton.hidden = NO;
        _textField.hidden = NO;
        _chooseButton.hidden = NO;
    }else{
        _chooseButton.hidden = YES;
        _textField.hidden = YES;
        _addButton.hidden = YES;
        _subtractButton.hidden = YES;
    }
}

-(UILabel*)createLabelWithFrame:(CGRect)frame text:(NSString*)text font:(int)font textAli:(NSTextAlignment)alig textColor:(UIColor*)tc{
    if(text == nil)
        text = @"";
    UILabel* label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.font = [UIFont systemFontOfSize:font];
    label.textAlignment = alig;
    label.textColor = tc;
    label.numberOfLines = 0;
    label.backgroundColor = [UIColor clearColor];
    return label;
}
@end
